package com.atguigu.tingshu.payment.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.payment.service.WxPayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;

    //api/payment/wxPay/createJsapi/1301/1914276682114555904
    //1301:表示 使用微信支付的类型， 订单支付；1302: 表示充值支付
    //1914276682114555904:订单编号

    //响应数据：！！
    @Operation(summary = "微信支付:拉起微信收银台")
    @PostMapping("createJsapi/{payType}/{orderNo}")
    @GuiguLogin
    public Result createJsapi(@PathVariable("payType") String payType,
                              @PathVariable("orderNo") String orderNo) {
        JSONObject jsonObject = wxPayService.createJsapi(payType, orderNo);
        return Result.ok(jsonObject);
    }

    @Operation(summary = "微信支付：异步回调接口")//不能加登录注解
    @PostMapping("notify")
    public Result wxPayNotify(HttpServletRequest request ,
                              @RequestBody Map<String,Object> params){
        /*测试数据
        System.out.println("Wechatpay-Serial");
        System.out.println("Wechatpay-Signature");
        System.out.println("Wechatpay-Timestamp");
        System.out.println("Wechatpay-Nonce");

        System.out.println("params = " + params);*/

        wxPayService.wxPayNotify(request , params);
        return Result.ok();
    }

    @Operation(summary = "查询wx支付的状态")
    @GetMapping("queryPayStatus/{orderNo}")
    @GuiguLogin
    public Result queryPayStatus(@PathVariable("orderNo")String orderNo){
        return Result.ok(wxPayService.queryPayStatus(orderNo));
    }

    @Operation(summary = "退款接口")
//    @GuiguLogin
    @PostMapping("refund/{orderNo}")
    public Result refund(@PathVariable("orderNo") String orderNo  ){
        wxPayService.refund(orderNo);
        return Result.ok();
    }

}
